package com.teacold.customdemo.chapter_five;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.view.View;

import com.teacold.customdemo.R;

public class GetPosTanView extends View {
    private Path mCircPath,mDstPath;
    private Paint mPaint;
    private PathMeasure mPathMeasure;
    private Float mCurAnimValue;
    private Bitmap mArrawBmp;

    private float[] pos=new float[2];
    private float[] tan=new float[2];


    public GetPosTanView(Context context) {
        super(context);
        setLayerType(LAYER_TYPE_SOFTWARE,null);
        mArrawBmp=BitmapFactory.decodeResource(getResources(), R.mipmap.navigation);

        mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);
        mPaint.setColor(Color.BLACK);
        mDstPath=new Path();
        mCircPath=new Path();
        mCircPath.addCircle(100,100,50,Path.Direction.CW);
        mPathMeasure=new PathMeasure(mCircPath,true);
        ValueAnimator valueAnimator=ValueAnimator.ofFloat(0,1);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mCurAnimValue= (Float) animation.getAnimatedValue();
                invalidate();
            }
        });
        valueAnimator.setDuration(3000);
        valueAnimator.start();

    }

    public GetPosTanView(Context context,  AttributeSet attrs) {
        this(context);
    }

    public GetPosTanView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float stop=mPathMeasure.getLength()*mCurAnimValue;
        mDstPath.reset();
        mPathMeasure.getSegment(0,stop,mDstPath,true);
        canvas.drawPath(mDstPath,mPaint);
        //旋转箭头图片并绘制
        mPathMeasure.getPosTan(stop,pos,tan);
        float degrees=(float)(Math.atan2(tan[1],tan[0])*180/Math.PI);
        Matrix matrix=new Matrix();

        //        getMatrix函数
//        matrix.postRotate(45,mArrawBmp.getWidth()/2,mArrawBmp.getHeight()/2);
//        matrix.postRotate(degrees,mArrawBmp.getWidth()/2,mArrawBmp.getHeight()/2);
//        matrix.postTranslate(pos[0]-mArrawBmp.getWidth()/2,pos[1]-mArrawBmp.getHeight()/2);
//        canvas.drawBitmap(mArrawBmp,matrix,mPaint);


        //getMatrix函数
        mPathMeasure.getMatrix(stop,matrix,PathMeasure.POSITION_MATRIX_FLAG|PathMeasure.TANGENT_MATRIX_FLAG);
        matrix.preTranslate(-mArrawBmp.getWidth()/2,-mArrawBmp.getHeight()/2);
        canvas.drawBitmap(mArrawBmp,matrix,mPaint);



    }
}
